Test Setup Failed
Push — master ( cd1dab...2a689d )
by
unknown
03:51
created

content-variant-collection-component.js ➔ define   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 80

Duplication

Lines 80
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 4
nop 1
dl 80
loc 80
rs 8.8387
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A content-variant-collection-component.js ➔ ... ➔ BaseComponent.extend.onAdd 23 23 3
A content-variant-collection-component.js ➔ ... ➔ BaseComponent.extend.validateContainer 7 7 2
A content-variant-collection-component.js ➔ ... ➔ BaseComponent.extend.onRemove 7 7 1
A content-variant-collection-component.js ➔ ... ➔ ContentVariantCollectionComponent 3 3 1
A content-variant-collection-component.js ➔ ... ➔ BaseComponent.extend.initialize 10 10 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1 View Code Duplication
define(function(require) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    'use strict';
3
4
    var ContentVariantCollectionComponent;
5
    var _ = require('underscore');
6
    var $ = require('jquery');
7
    var mediator = require('oroui/js/mediator');
8
    var BaseComponent = require('oroui/js/app/components/base/component');
9
10
    ContentVariantCollectionComponent = BaseComponent.extend({
11
        options: {
12
            buttonSelector: '[data-role="variant-button"]',
13
            variantRemoveSelector: '[data-action="remove"]',
14
            collectionContainerSelector: '[data-role="collection-container"]'
15
        },
16
17
        /**
18
         * @inheritDoc
19
         */
20
        constructor: function ContentVariantCollectionComponent() {
21
            ContentVariantCollectionComponent.__super__.constructor.apply(this, arguments);
22
        },
23
24
        /**
25
         * @inheritDoc
26
         */
27
        initialize: function(options) {
28
            this.options = _.defaults(options || {}, this.options);
29
30
            this.$el = this.options._sourceElement;
31
32
            this.$el.on('click', this.options.buttonSelector, _.bind(this.onAdd, this));
33
            this.$el.on('click', this.options.variantRemoveSelector, _.bind(this.onRemove, this));
34
            this.prototypeName = this.$el.data('prototype-name') || '__name__';
35
            this.$collectionContainer = this.$el.find(this.options.collectionContainerSelector);
36
        },
37
38
        onAdd: function(e) {
39
            e.preventDefault();
40
41
            var $button = $(e.currentTarget);
42
            if ($button.attr('disabled')) {
43
                return;
44
            }
45
46
            var prototype = this.$el.data('prototype');
47
            if (prototype) {
48
                var index = parseInt(this.$el.data('last-index'));
49
                var nextItemHtml = prototype.replace(new RegExp(this.prototypeName, 'g'), index);
50
51
                this.$collectionContainer
52
                    .prepend(nextItemHtml)
53
                    .trigger('content:changed');
54
                this.$el.data('last-index', ++index);
55
56
                this.validateContainer();
57
            }
58
59
            mediator.trigger('cms:content-variant-collection:add', this.$el);
60
        },
61
62
        onRemove: function(e) {
63
            e.preventDefault();
64
            var item = $(e.target).closest('*[data-content]');
65
            item.remove();
66
67
            mediator.trigger('cms:content-variant-collection:remove', this.$el);
68
        },
69
70
        validateContainer: function() {
71
            var $validationField = this.$el.find('[data-name="collection-validation"]:first');
72
            var $form = $validationField.closest('form');
73
            if ($form.data('validator')) {
74
                $form.validate().element($validationField.get(0));
75
            }
76
        }
77
    });
78
79
    return ContentVariantCollectionComponent;
80
});
81